package com.faithbj.danaliverpool.web.controller.admin;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.faithbj.common.configuration.Pager;
import com.faithbj.danaliverpool.service.SystemLogInfoService;

/**
 * @author jrn E-mail: rinanjin@gmail.com
 * @version 0.1 
 * 创建时间：2012-6-26 下午11:25:52
 * 类说明
 */
@Controller
@RequestMapping(value = "/faith/systemlog")
public class AdminSystemLogInfoController extends BaseAdminController
{
	private static final long serialVersionUID = 1L;

	private Logger logger = LoggerFactory.getLogger(AdminSystemLogInfoController.class);
	
	private final String LIST = "/admin/system_log_list";
	
	@Resource
	private SystemLogInfoService systemLogInfoService;
	
	@RequestMapping(value = "list")
	public String list(Pager pager, String username, String beginDate, String endDate, ModelMap map)
	{
		pager.setPropertyList(new ArrayList<String>());
		pager.setCompareTypeList(new ArrayList<String>());
		pager.setKeywordList(new ArrayList<Object>());
		if(username != null && !"".equals(username.trim()))
		{
			pager.getPropertyList().add("username");
			pager.getCompareTypeList().add("like");
			pager.getKeywordList().add(username);
			map.put("username", username);
		}
		
		if(beginDate != null && !"".equals(beginDate.trim()))
		{
			pager.getPropertyList().add("createDate");
			pager.getCompareTypeList().add("ge");
			pager.getKeywordList().add(parseStr2Date(beginDate));
			map.put("beginDate", beginDate.toString());
		}
		
		if(endDate != null && !"".equals(endDate.trim()))
		{
			pager.getPropertyList().add("createDate");
			pager.getCompareTypeList().add("le");
			pager.getKeywordList().add(parseStr2Date(endDate));
			map.put("endDate", endDate.toString());
		}
		
		Pager pagerVal = systemLogInfoService.findByPager(pager);

		map.put("pager", pagerVal);
		return LIST;
	}
	
	private Date parseStr2Date(String dateStr)
	{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try
		{
			Date date = sdf.parse(dateStr);
			return date;
		}
		catch (ParseException e)
		{
			logger.error(e.getMessage());
		}
		return null;
	}
}
